/*
 * 作者: 侯晓宇
 * 时间: 2017-02-16 
 * 说明：
 * Copyright (c) 2016-2017 中研沃兮
 */

define(function(require) {

	require("jquery");

	var c = require('app/utils/CommonUtils');
	var EventUtils = require('app/utils/EventUtils');
	var appInfo = require('app/AppInfo');
	var action = require('app/action/RoomWinAction');
	var UserInfoUtils = require('app/utils/UserInfoUtils');
	var APIUtils = require('app/utils/APIUtils');
	var hc = require('app/utils/HttpClientUtils');
	var param = api.pageParam;
	
	//禁止休眠
	api.setKeepScreenOn({
    	keepOn: true
	});
	api.openFrame({
	    name: 'RoomStagingFrm',
	    url: './RoomStagingFrm.html',
	    pageParam: param
    });
	
	//获取发送给room的msg
	EventUtils.listen(api.winName,'show_room',false,function(){
		
		action.fetchLecture()
		.then(function(lecture){
			//获取视频或者图片信息
			param = $.extend({},param,lecture);
			c.l(param);
			
			var player_h = parseInt(APIUtils.winW() * 9 / 16);
			
			//启动播放页面
			//主讲人信息，ppt信息，直播信息，chair信息等
			
			api.openFrame({
			    name: 'RoomPlayerFrm',
			    url: './RoomPlayerFrm.html',
			    pageParam: param,
			    rect: {
			    	x: 0,
			    	y: 0,
			    	w: APIUtils.winW(),
			    	h: player_h
			    }
		    });
		    api.bringFrameToFront({from: 'RoomStagingFrm'});
		    
		    //顶部菜单栏 高度为2.2rem
		    api.openFrame({
			    name: 'RoomHeaderFrm',
			    url: './RoomHeaderFrm.html',
			    pageParam: param,
			    rect: {
			    	x: 0,
			    	y: 0,
			    	w: APIUtils.winW(),
			    	h: parseInt(c.remToPx(2.2))
			    },
			    bgColor:'rgba(0,0,0,0)'
		    });
		    api.bringFrameToFront({from: 'RoomStagingFrm'});
		    
		    //中部，在线用户、参与人数、详情
		    var middle_y = parseInt(APIUtils.winW() * 9 /16)
		    var middle_h = parseInt(c.remToPx(2.2));
		    api.openFrame({
		        name: 'RoomMiddleFrm',
		        url: './RoomMiddleFrm.html',
		        pageParam: param,
		        vScrollBarEnabled: false,
		        hScrollBarEnabled: false,
		        rect: {
			        x:0,
			        y:middle_y,
			        w:APIUtils.winW(),
			        h:middle_h
		        }
	        });
	        api.bringFrameToFront({from: 'RoomStagingFrm'});
	        
	        //底部操作区, 2.2rem;
	        var footer_h = parseInt(c.remToPx(2.2));
	        var footer_y = APIUtils.winH() - footer_h;
	        api.openFrame({
		        name: 'RoomFooterFrm',
		        url: './RoomFooterFrm.html',
		        pageParam: param,
		        rect: {
			        x:0,
			        y:footer_y,
			        w:APIUtils.winW(),
			        h:footer_h
		        }
	        });
	        api.bringFrameToFront({from: 'RoomStagingFrm'});
	        
	        //对话区
	        var chat_y = middle_y + middle_h;
	        var chat_h = APIUtils.winH() - player_h - footer_h - middle_h;
	        
			api.openFrame({
		        name: 'RoomChatBodyFrm',
		        url: './RoomChatBodyFrm.html',
		        pageParam: param,
		        bounces: true,
		        rect: {
			        x:0,
			        y:chat_y,
			        w:APIUtils.winW(),
			        h:chat_h
		        }
	        });
	        api.bringFrameToFront({from: 'RoomStagingFrm'});
	        
	        setTimeout(
	        	function(){
	        		action.wsinit();
	        	},
	        	1000
	        );
		        
		});
			
		
		
		
	});

	
	
	
	//获取发送给room的msg
	EventUtils.listen(api.winName,'send_room_msg',false,function(data){
		action.wssend(data);
	});
	
	EventUtils.listen(api.winName,'close_staging_frm',false,function(data){
		api.closeFrame({
		    name: 'RoomStagingFrm'
		});
	});
	
//
//	EventUtils.listen(api.winName,'close_stream_publish',false,function(data){
//		location.reload();
//	});
//

	//设置竖屏
	api.setScreenOrientation({
    	orientation: 'portrait_up'
	});
	api.addEventListener({
    	name: 'keyback'
	}, function(ret, err) {
		
	   	api.setScreenOrientation({
    		orientation: 'portrait_up'
		});
		EventUtils.send('root', 'reset');
		api.closeWin({
        });
	});	
	
	
	var pwd = param.pwd;
	if(c.isNullStr(pwd) || pwd == false){
		//go on;
		if(param.doortemplate=="enroll"){
			function enroll(){
				var useruuid=UserInfoUtils.getLoginedUser().userid;
				var roomuuid=param.uuid;
				var url =appInfo.serverbase+'/luyan/form/room/' + roomuuid + '/user/' + useruuid + '/autocheck';
				var body={};
				hc.post(url,body,function(resp){
					if(resp.status=="200"&&resp.result.next){
						EventUtils.send('RoomWin','close_staging_frm');
						EventUtils.send(api.winName,'show_room');
					}else{
						api.openFrame({
						    name: 'RoomEnrollVerifyFrm',
						    url: './RoomEnrollVerifyFrm.html',
						    bgColor: 'rgba(0,0,0,0)',
						    pageParam: param
					    });
					}
				})
			}
			enroll()
		}else if(param.doortemplate=="regist"){
			function regist(){
				var useruuid=UserInfoUtils.getLoginedUser().userid;
				var roomuuid=param.uuid;
				var url =appInfo.serverbase+'/luyan/form/room/' + roomuuid + '/user/' + useruuid + '/autocheck_2';
				var body={};
				hc.post(url,body,function(resp){
					if(resp.status=="200"&&resp.result.next){
						EventUtils.send('RoomWin','close_staging_frm');
						EventUtils.send(api.winName,'show_room');
					}else{
						api.openFrame({
						    name: 'RoomRegistFrm',
						    url: './RoomRegistFrm.html',
						    bgColor: 'rgba(0,0,0,0)',
						    pageParam: param
					    });
					}
				})
			}
			regist()
		}else{
			EventUtils.send('RoomWin','close_staging_frm');
			EventUtils.send(api.winName,'show_room');
		}
		//是否关联报名表
		//0. param.door == "enroll"
		//0. param.uuid -> roomuuid

		
		
		//1. 判断当前用户是否已经报名确认过
		//UserInfoUtils.getLoginedUser().userid
		//var url = '/luyan/form/room/' + roomuuid + '/user/' + useruuid + '/autocheck';
		//var body = {};
		//if true close_staging_frm,show_room
		//resp.result.next is true , 已经报名过，
				
		
		//if false api.openFrame(RoomEnrollVerifyFrm)

		//in RoomEnrollVerifyFrm.js
		//2.通过预留的邮箱和预留电话，判断是否已经报名过
		//有两个值，一个是邮件地址、一个手机号
//				body.enrolltel = this.refs.tel.getValue();
//		body.enrollemail = this.refs.email.getValue();
		//'/luyan/form/room/' + roomuuid + '/enrollcheck/custom_1';
		//resp.result.next
		//如果报名过，则 close_staging_frm,show_room
		
		//EventUtils.send(api.winName,'show_room');
				
	}else{
//		if(param.creatoruuid == UserInfoUtils.getLoginedUser().userid){
//			EventUtils.send(api.winName,'show_room');
//		}else{
			//PWD
			api.openFrame({
			    name: 'RoomPWDVerifyFrm',
			    url: './RoomPWDVerifyFrm.html',
			    bgColor: 'rgba(0,0,0,0)',
			    pageParam: param
		    });
//		}
		
	}
	
	
	return;
}); 